package R.Service;

import R.Dao.RegDao;
import R.Entity.Record;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "RegShowAllServlet", value = "/RegShowAll")
public class RegShowAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        List<Record> list = RegDao.showAll();
        if (list != null) {
            request.setAttribute("RegList", list);//在请求过程中绑定
            request.getRequestDispatcher("/R/regShowAll.jsp").forward(request, response);//请求转发
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        /*HttpSession session = request.getSession();
        if (session.getAttribute("loginName")==null){
            response.sendRedirect("/Test/dormLogin.jsp");
        }*/
        String RegistrationID = request.getParameter("RegistrationID");
        String CardID = request.getParameter("CardID");
        if (!CardID.equals("") && !RegistrationID.equals("")) {
            Record r = new Record();
            r.setCardID(CardID);
            r.setRegistrationID(RegistrationID);
            List<Record> list = RegDao.ChaXun(r);
            if (list != null) {
                request.setAttribute("RegList", list);//在请求过程中绑定
                request.getRequestDispatcher("/R/regShowAll.jsp").forward(request, response);//请求转发
            }
        } else {
            List<Record> list = RegDao.showAll();
            if (list != null) {
                request.setAttribute("RegList", list);//在请求过程中绑定
                request.getRequestDispatcher("/R/regShowAll.jsp").forward(request, response);//请求转发
            }
        }
    }
}
